import React from 'react';
import {Button, Form, Input, Select, message, AutoComplete} from 'antd';
import qs from 'qs';
import * as Constant from '../../../../common/const';
import * as Request from '../../../../Server/Upgrade';

const FormItem = Form.Item;
const TextArea = Input.TextArea;
const Option = Select.Option;
const idValue = [
    'totalbandwidth','queuemax','manualkeep','clientovertime',
    'usefluxcontrol','allowcounteveryday','verifyoverTime','verifymax',
    'maxspeed','returnstatusinfo','returndebuginfo','manualpatchkeep'
];

class SettingCommonForm extends React.Component {
    constructor(props) {
        super(props);
    }

    handleSubmit = (e) => {
        e.preventDefault();
        this.props.form.validateFieldsAndScroll((err, values) => {
            if (!err) {
                const request = this.props.settingCommon ?  Request.settingCommonUpdate :  Request.settingCommonSave;
                values.fun = values.fun?values.fun:'';
                values.idMain = values.idMain.replace(/\s*/g,"");
                request(qs.stringify(values)).then(res => {
                    if (Constant.REQUEST_SUCCESS == res.code) {
                        message.success(Constant.OPERATION_SUCCESS_TIP, Constant.AUTO_DISAPPEAR_TIME);
                        this.props.cancel(true);
                    }else{
                        res.code !== Constant.REQUEST_UNAUTHORIZED && message.error(res.msg,2);
                    }
                });
            }
        });
    }
    validator = (rule, value, callback) => {
        const id = this.props.form.getFieldValue('id');
        let params = {idMain: value};
        id && (params.id = id);
        if(value.length>255){
            callback('设置ID长度不能超过255个字符');
            return ;
        }
        if(value && value.length){
            Request.settingCommonValidator(qs.stringify(params)).then(res => {
                if (Constant.REQUEST_SUCCESS == res.code) {
                    if (res.data) {
                        callback();
                    } else {
                        callback('验证不通过');
                    }
                } else {
                    callback(res.msg);
                }
            })
        }else{
            callback();
        }
    }

    //校验参数方法
    checkParam = (rule, value, callback) =>{
        let idMain = this.props.form.getFieldValue('idMain');
        if(!idMain){
            callback();
            return ;
        }
        let id = idMain.toLowerCase();
        if(id == 'manualkeep'
        ||id == 'manualpatchkeep'
        ||id == 'totalbandwidth'
        ||id == 'queuemax'
        ||id == 'clientovertime'
        ||id == 'patchkeep'
        ||id == 'maxspeed'
        ||id == 'countpsforpatchupdate'
        ||id == 'countpsforselfupdate'
        ||id == 'verifymax'
        ||id == 'verifyovertime'
        ){
            if(value==''||value==undefined){
                callback('参数不能为空!');
                return ;
            }
            if(isNaN(value)){
                callback('参数必须为数字!不能含有空格或其他字符!');
                return ;
            }
        }
        if(id == 'returnstatusinfo'
        ||id == 'returndebuginfo'
        ||id == 'forceupdate'){
            if(value!='true'&&value!='false'){
                callback('参数必须为true或false!不能含有空格或其他字符!');
                return ;
            }
        }
        callback();
    }

    render() {
        const {getFieldDecorator} = this.props.form;
        const ipFormItemLayout = {
            labelCol: {
                xs: {span: 24},
                sm: {span: 6},
            },
            wrapperCol: {
                xs: {span: 24},
                sm: {span: 15},
            },
        };
        const tailFormItemLayout = {
            wrapperCol: {
                xs: {
                    span: 24,
                    offset: 0,
                },
                sm: {
                    span: 16,
                    offset: 8,
                },
            },
        };

        return (
            <div>
                <Form onSubmit={this.handleSubmit}>
                    <FormItem>
                        {getFieldDecorator('id', {
                            initialValue: this.props.settingCommon && this.props.settingCommon.id,
                        })(
                            <Input type={'hidden'}/>
                        )}
                    </FormItem>
                    <FormItem label={'设置ID:'} {...ipFormItemLayout} >
                        {getFieldDecorator('idMain', {
                            rules: [{
                                required: true, message: '请输入设置ID!',
                            },{
                                validator: this.validator,
                            }],
                            initialValue: this.props.settingCommon && this.props.settingCommon.idMain,
                        })(
                            <AutoComplete
                                dataSource={idValue}
                                style={{ width: '100%' }}
                            />
                        )}
                    </FormItem>
                    <FormItem label={'函数:'} {...ipFormItemLayout} >
                        {getFieldDecorator('fun', {
                            initialValue: this.props.settingCommon && this.props.settingCommon.fun,
                        })(
                            <Select
                                showSearch
                                placeholder="请选择函数"
                                optionFilterProp="children"
                                filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                            >
                                <Option value="checkinterval">checkinterval</Option>
                                <Option value="denytimerange">denytimerange</Option>
                                <Option value="dreg">dreg</Option>
                                <Option value="fluxinterval">fluxinterval</Option>
                                <Option value="maxspeed">maxspeed</Option>
                                <Option value="infosvr">infosvr</Option>
                                <Option value="internal">internal</Option>
                                <Option value="param">param</Option>
                                <Option value="sendinfo">sendinfo</Option>
                                <Option value="statesvr">statesvr</Option>
                                <Option value="tasksetting">tasksetting</Option>
                                <Option value="wreg">wreg</Option>
                                <Option value="wxml">wxml</Option>
                                <Option value="wini">wini</Option>
                                <Option value="wtempstatus">wtempstatus</Option>
                                <Option value="wpslink">wpslink</Option>
                            </Select>
                        )}
                    </FormItem>
                    <FormItem label={'参数:'} {...ipFormItemLayout} >
                        {getFieldDecorator('param', {
                            rules: [{ validator: this.checkParam }],
                            initialValue: this.props.settingCommon && this.props.settingCommon.param,
                        })(
                            <Input maxLength={255}/>
                        )}
                    </FormItem>
                    <FormItem label={'备注:'} {...ipFormItemLayout} >
                        {getFieldDecorator('remark', {
                            initialValue: this.props.settingCommon && this.props.settingCommon.remark,
                        })(
                            <TextArea  rows={4} cols={30}
                            placeholder={'备注,250字以内'}
                            maxlength={250}
                            />
                        )}
                    </FormItem>
                    <FormItem {...tailFormItemLayout}>
                        <Button type="primary" htmlType="submit">保存</Button>
                        <Button type="primary" onClick={this.props.cancel}>返回</Button>
                    </FormItem>
                </Form>
            </div>
        );
    }
}

const RegisterSettingCommonForm = Form.create()(SettingCommonForm);
export default RegisterSettingCommonForm;